﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class RegisterCourseAR : System.Web.UI.Page
{


    public string backLink
    {
        get
        {

            if (Request["Page"] != null)
                return "./HomeAR.aspx?Rand=" + System.Guid.NewGuid().ToString();
            else
                return "./CatalogPageAR.aspx?Rand=" + System.Guid.NewGuid().ToString(); ;
            
        }
    }



    private Courses mObjCourses
    {
        get
        {
            if (ViewState["mObjCourses"] == null)
            {
                ViewState["mObjCourses"] = System.Guid.NewGuid().ToString();
            }

            if (Session[ViewState["mObjCourses"].ToString()] == null)
            {
                Session[ViewState["mObjCourses"].ToString()] = new Courses();
            }

            return (Courses)Session[ViewState["mObjCourses"].ToString()];


        }


        set
        {
            if (ViewState["mObjCourses"] == null)
            {
                ViewState["mObjCourses"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["mObjCourses"].ToString()]=value;


        }


    }



    protected void Page_Load(object sender, EventArgs e)
    {




        if (!Page.IsPostBack)
        {

            checkSecurityAndCustomizePage();


            bindAllControls();

            showLanguage();

            Page.DataBind();

            
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), "HideBackButton", "hideBackButton();", true);
    }



    /// <summary>
    /// this function will get if the language filter will shown or not
    /// </summary>
    private void showLanguage()
    {
        SystemOptionsDB objDB = new SystemOptionsDB(ConnectionStrings.LMS);
        List<SystemOptions> lst = objDB.Select("", "");




        if (lst.Count < 2)
        {

            dvCourseTime.Visible = true;
        }
        else
        {


            dvCourseTime.Visible = lst[1].ShowCourseLanguage;
        }

    }


    /// <summary>
    /// this function will bind all the controls of the page
    /// </summary>
    private void bindAllControls()
    {
        txtCourseNameEN.Text = mObjCourses.CourseNameAR;
        txtDuration.Text = GlobalVars.setSecounds (mObjCourses.Duration);
        txtObjectivesEN.Text = mObjCourses.ObjectivesAR;
        txtOutlineEN.Text = mObjCourses.OutlineAR;
        txtTargetEN.Text = mObjCourses.TargetAR;

        txtCourseNameEN.Text = (txtCourseNameEN.Text.Trim() == "") ? "غير متوفر" : txtCourseNameEN.Text;
        txtDuration.Text = (txtDuration.Text.Trim() == "") ? "غير متوفر" : txtDuration.Text;
        txtObjectivesEN.Text = (txtObjectivesEN.Text.Trim() == "") ? "غير متوفر" : txtObjectivesEN.Text;
        txtOutlineEN.Text = (txtOutlineEN.Text.Trim() == "") ? "غير متوفر" : txtOutlineEN.Text;
        txtTargetEN.Text = (txtTargetEN.Text.Trim() == "") ? "غير متوفر" : txtTargetEN.Text;
    }

    /// <summary>
    /// this function will check the user previliges and user registered courses and customize page
    /// </summary>
    protected void checkSecurityAndCustomizePage()
    {
        Persons currentPerson = GlobalVars.getCurrentPersonAR();
        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Catalog_Page))
        {
            Response.Redirect("loginAR.aspx");
            return;
        }


        btnRegister.Visible = false;
        btnStartCourse.Visible = true;
        pnlExplain.Visible = true;
        if (Request["ID"] == null)
        {
            Response.Redirect("CatalogPageAR.aspx");
            return;
        }

        long CourseID = 0;
        if (!long.TryParse(Request["ID"].ToString(), out CourseID))
        {
            Response.Redirect("CatalogPageAR.aspx");
            return;

        }


        CoursesDB objCoursesDB = new CoursesDB(ConnectionStrings.LMS);

        List<Courses> lstCourses = objCoursesDB.getCoursesByID(CourseID);


        if (lstCourses.Count == 0)
        {
            Response.Redirect("CatalogPageAR.aspx");
            return;
        }



        mObjCourses = lstCourses[0];


        if (!currentPerson.lstCourses.Contains(lstCourses[0]))
        {
            btnRegister.Visible = true;
            btnStartCourse.Visible = false;
            pnlExplain.Visible = false;
        }
        else
        {
            btnRegister.Visible = false;
            btnStartCourse.Visible = true;
            pnlExplain.Visible = true;
        }

        bindStartCourse();
    }


    protected void btnRegister_Click(object sender, EventArgs e)
    {
        Persons currentPerson = GlobalVars.getCurrentPersonAR();
        
        if (Request["ID"] == null)
        {
            lblError.Text = "خطأ , من فضلك حاول مرة اخرى !";
            return;
        }
        
        long CourseID = 0;
        if (!long.TryParse(Request["ID"].ToString(),out CourseID))
        {
            lblError.Text = "خطأ من فضلك حاول مرة اخرى !";
            return;

        }


        CoursesDB objCoursesDB = new CoursesDB(ConnectionStrings.LMS);

        List<Courses> lstCourses = objCoursesDB.getCoursesByID(CourseID);


        if (lstCourses.Count == 0)
        {
            lblError.Text = "خطأ من فضلك حاول مرة اخرى !";
            return;
        }


        if (currentPerson.lstCourses.Contains(lstCourses[0]))
        {
            lblError.Text = "الدورة مسجلة من قبل.";
            btnRegister.Visible = false;
            btnStartCourse.Visible = true;
            pnlExplain.Visible = true;
            return;
        }



        Courses currentCourses = lstCourses[0];

        RegisteredCoursesDB objRegisteredCoursesDB = new RegisteredCoursesDB(ConnectionStrings.LMS);

        RegisteredCourses objRegisteredCourses = new RegisteredCourses();
        objRegisteredCourses.CoursesID = currentCourses.ID;
        objRegisteredCourses.CourseStatus = (long)CourseEnum.Learning;
        objRegisteredCourses.EmployeesID = currentPerson.GenID;
        objRegisteredCourses.StartDate = DateTime.Now;


        objRegisteredCoursesDB.Insert(objRegisteredCourses);

        GlobalVars.addLog(objRegisteredCourses.ID, 1);
        GlobalVars.sendLogMail(objRegisteredCourses, currentPerson, 1);

        lblResult.Text = "تم التسجيل بنجاح.";

        btnRegister.Visible = false;
        btnStartCourse.Visible = true;
        pnlExplain.Visible = true;               
        


    }
    protected void bindStartCourse()
    {
        long CourseID = -1;

        try { CourseID = long.Parse(Request["ID"]); }
        catch { Response.Redirect("LoginAR.aspx"); }

        

            btnStartCourse.NavigateUrl = "start_learn_frame.aspx?id=" + CourseID.ToString();
            btnStartCourse.Target = "_blank";
        

        


       
    }
}